/*
 * CoordExtractWithMarker.cpp
 * Created on: 04/04/2011
 * Author: catharine
 */

#include "ExtratorDeCamposComMarcadores.h"

ExtratorDeCamposComMarcadores::ExtratorDeCamposComMarcadores() {
}

ExtratorDeCamposComMarcadores::~ExtratorDeCamposComMarcadores() {
}

Vector <pair<int[4], bool> > ExtratorDeCamposComMarcadores::encontrarCampos(IplImage* sum){

                                                            }
/*
 * Encontra os valores referentes aos marcadores verticais.
 * @param img The image with the markers.
 * @param verticalSizes Array of length of vertical markers. Minimum size of the array should be 10.
 * @param verticalInitial Array with the initial positions of the vertical markers. Minimum size of the array should be 10.
 * @param vSize The minimum size of the arrays.
 */
void ExtratorDeCamposComMarcadores::verticalValues(IplImage* img, int* verticalSizes,
		int* verticalInitial, int vSize) {
	int height = img->height;
	int counter = 0;
	for (int x = 2; x < 6; x++) {
		for (int y = 2; y < height; y++) {
			int size = 0;
			if (cvGetReal2D(img, y, x) == 0) {
				if (counter < vSize) {
					verticalInitial[counter] = y;
				}

				while (true) {
					if (y < height - 1 && cvGetReal2D(img, y, x) == 0) {
						y++;
						size++;
					} else {
						break;
					}
				}

				if (counter < vSize) {
					verticalSizes[counter] = size;
				}
				counter++;
			}
		}
	}
}

/*
 * Encontra os valores referentes aos marcadores horizontais.
 * @param img The image with the markers.
 * @param horizontalSizes Array of length of horizontal markers. Minimum size of the array should be 16.
 * @param horizontalInitial Array with the initial positions of the horizontal markers. Minimum size of the array should be 16.
 * @param hSize The minimum size of the arrays.
 */
void ExtratorDeCamposComMarcadores::horizontalValues(IplImage * img,
		int * horizontalSizes, int * horizontalInitial, int hSize) {
	int width = img->width;
	int counter = 0;
	for (int y = 2; y < 6; y++) {
		for (int x = 2; x < width; x++) {
			int size = 0;
			if (cvGetReal2D(img, y, x) == 0) {
				if (counter < hSize) {
					horizontalInitial[counter] = x;
				}

				while (true) {
					if (x < width - 1 && cvGetReal2D(img, y, x) == 0) {
						x++;
						size++;
					} else {
						break;
					}
				}

				if (counter < hSize) {
					horizontalSizes[counter] = size;
				}
				counter++;
			}
		}
	}
}

/*
 * Find the coordinates of the markers.
 * @oaram img The image with the markers.
 * @param horizontalSizes Array of length of horizontal markers. Minimum size of the array should be 16.
 * @param horizontalInitial Array with the initial positions of the horizontal markers. Minimum size of the array should be 16.
 * @param verticalSizes Array of length of vertical markers. Minimum size of the array should be 10.
 * @param verticalInitial Array with the initial positions of the vertical markers. Minimum size of the array should be 10.
 */
void ExtratorDeCamposComMarcadores::coordinates(IplImage * img, int* horizontalSizes,
		int* horizontalInitial, int* verticalSizes, int* verticalInitial) {

	IplImage * imageGS = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage * dst = cvCreateImage(cvGetSize(img), 8, 1);

	double percent = (770 / double(dst->width)) * 100;
	IplImage * dst2 = cvCreateImage(cvSize(
			(int) ((dst->width * percent) / 100),
			(int) ((dst->height * percent) / 100)), 8, 1);

	cvCvtColor(img, imageGS, CV_RGB2GRAY);

	cvAdaptiveThreshold(imageGS, dst, 255, CV_ADAPTIVE_THRESH_MEAN_C,
			CV_THRESH_BINARY, 151, 5);

	cvResize(dst, dst2);

	horizontalValues(dst2, horizontalSizes, horizontalInitial, 16);
	verticalValues(dst2, verticalSizes, verticalInitial, 10);

	cvReleaseImage(&imageGS);
	cvReleaseImage(&dst);
	cvReleaseImage(&dst2);
}

